/*
 * @Description:板子管理
 * @Author: maskMan
 * @LastEditTime: 2024-03-20 18:37:09
 */
import { _decorator, Component, Node, Prefab, instantiate, Vec3, v3, macro } from "cc";
import { ball } from "./ball";
import { board } from "./board";
import { Constants } from "./utils/constants";
import { utils } from "./utils/utils";
const { ccclass, property } = _decorator;
const _tempPos = new Vec3();
@ccclass("bordManger")
export class bordManger extends Component {
  @property(Prefab)
  boardPre: Prefab = null;
  @property(Prefab)
  ballPre: Prefab = null;
  @property(Node)
  boardContent: Node = null;
  @property(Node)
  ballContent: Node = null;
  lastBoardIndex: number = 0;
  start() {
    this.initBoard();
  }
  boardList: board[] = [];
  //初始化板子
  initBoard() {
    for (let index = 0; index < Constants.BOARD_NUM; index++) {
      const pre = instantiate(this.boardPre);
      const sc = pre.getComponent("board") as board;
      sc.index = index + 1;
      this.boardContent.addChild(pre);
      this.boardList.push(sc);
    }
    this.scheduleOnce(this.createRole, 0);
  }
  getLastBord(): board {
    let temp: board;
    let max = macro.REPEAT_FOREVER;
    for (let i = 0; i < this.boardList.length; i++) {
      const item = this.boardList[i];
      if (item.node.getPosition().y < max) {
        max = item.node.getPosition().y;
        temp = item;
      }
    }
    return temp;
  }
  currBall: ball;
  createRole() {
    const pre = instantiate(this.ballPre);
    this.ballContent.addChild(pre);
    const pos = this.getLastBord().node.getPosition();
    pre.setPosition(v3(pos.x, pos.y + 0.6, 3));
    this.currBall = pre.getComponent("ball") as ball;
  }
  //设置板子坐标
  resetBoard(board: Node) {}
  update(deltaTime: number) {}
}
